<?php


namespace app\manager\controller;


use app\common\controller\Common;
use app\common\model\Management;
use app\common\model\Users;
use think\Db;
use think\facade\Request;
use think\facade\Session;

class Manager extends Common
{

    public function lists(Management $management)
    {
        if ($this->request->isAjax()) {
            $page = $this->getPage();
            $pagesize = $this->getPageSize();
            $min = ($page - 1) * $pagesize;
            $max = $pagesize;
            $username = Session::get('manager');
            if ($username == 'admin') {
                $sql = "select * from management";
            } else {
                $sql = "select * from management where username='" . $username."'";
            }
            $data = Db::query($sql);
            $reManager = Db::query($sql . " limit " . $min . "," . $max);
            return $this->listJson($reManager, count($data));
        } else {
            $username = Session::get('manager');
            $this->assign('username',$username);
            return view();
        }
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $Data = Request::param();
            //用户名是否重复
            $user = Management::where('username', '=', $Data['username'])->find();
            if ($user) return $this->errJson('用户名已存在,请重新输入');
            //设置密码
            if ($Data['pwd1'] != $Data['pwd2']) {
                return $this->errJson('密码输入不一致');
            }
            $userData['password'] = password_hash($Data['pwd1'], PASSWORD_DEFAULT);
            $userData['username']=$Data['username'];
            $res = Management::create($userData);
            if ($res) {
                return $this->okJson('添加成功');
            } else {
                return $this->errJson('添加失败');
            }
        } else {
            return view();
        }
    }

    //编辑用户
    public function edit()
    {
        $manid = input('manid');
        if ($this->request->isAjax()) {
            $name = input('username');
            if (empty($name) || empty($manid)) return $this->errJson('缺少参数');
            $reManagers = Management::where(['username'=>$name])->find();
            if ($reManagers) return $this->errJson('用户名重复');
            $reManager = Management::get($manid);
            $reManager->username;
            if ($reManager->save()) {
                return $this->okJson('修改成功');
            }
            return $this->errJson('修改失败');
        } else {
            $manager = Management::get($manid);
            $this->assign('manager', $manager);
            return view();
        }
    }

    public function editPwd(Management $management)
    {
        $manid= input('manid');
        if ($this->request->isAjax()) {
            $oldPwd = input('oldPwd');
            $newPwd = input('newPwd');
            $newPwd1 = input('newPwd1');
            if (strcmp($newPwd,$newPwd1)!=0) {
                $this->errJson('密码不一致');
            }

            $managements= $management->where(['manid' => $manid])->find();
            if (empty($managements) || password_verify($oldPwd, $managements->password)==false) return $this->errJson('旧密码不正确');
            $pwd = password_hash($newPwd, PASSWORD_DEFAULT);
            $result = Management::update(['password' => $pwd], ['manid' => $manid]);
            if ($result) {
                session(null);
                return $this->okJson('修改成功');
            } else {
                return $this->errJson('失败');
            }
        } else {
            $this->assign('manid', $manid);
            return view();
        }
    }

    public function reset()
    {
        $manid = input('manid');
        $pwd = password_hash('123456', PASSWORD_DEFAULT);
        $result = Management::update(['password' => $pwd], ['manid' => $manid]);
        if ($result) {
            return $this->okJson('重置成功');
        } else {
            return $this->errJson('失败');
        }
    }

    public function editStatus()
    {
        $manid = input('manid');
        $status = input('status');

        $result = Management::update(['status' => $status], ['manid' => $manid]);
        if ($result) {
            return $this->okJson('成功');
        } else {
            return $this->errJson('失败');
        }
    }

    //删除
    public function del()
    {
        $manid = input('manid');
        $result = Management::destroy($manid);
        if ($result) return $this->okJson('ok');
        return $this->errJson('失败');
    }
}